package com.cx.base.day25_Rocketmq.producer.syncInfo;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class SyncBatchProducer {
    public static void main(String[] args) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        //        1.创建消息生产者，并且制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
//        设置超时时间
        producer.setSendMsgTimeout(10000);
//        2.指定namesrv地址
        producer.setNamesrvAddr("192.168.48.128:9876;192.168.48.137:9876");
//        producer.setNamesrvAddr("47.96.165.93:9876;106.52.131.197:9876");
//        3.启动producer
        try {
            producer.start();
        } catch (
                MQClientException e) {
            e.printStackTrace();
        }
//      4.将消息对象放到集合中
        List<Message> msgs = new ArrayList<>();
        Message msg1 = new Message("batchTopic", "batch", ("helloworld" + 1).getBytes());
        Message msg2 = new Message("batchTopic", "batch1", ("helloworld" + 2).getBytes());
        Message msg3 = new Message("batchTopic", "batch2", ("helloworld" + 3).getBytes());
        msgs.add(msg1);
        msgs.add(msg2);
        msgs.add(msg3);
//      5.发送集合msgs
        SendResult result = producer.send(msgs);
        SendStatus status = result.getSendStatus();
        System.out.println("发送结果:" + status);
        TimeUnit.SECONDS.sleep(1);
//         关闭producer
        producer.shutdown();
    }
}
